//我的二维码
var link;
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.loadData();
	$('#qrcode').empty();
	refreshCode(link + "?recommandPerson=" + storage.plainpassword + "&d=" + Math.random());
};
Main.prototype = {
	init: function() {
		mui('.mui-scroll-wrapper').scroll({
			indicators: true //是否显示滚动条
		});
		link = h5_root_url + '/page/register.html';
	},
	even: function() {
		/**刷新二维码*/
		$('#btn-refresh').on('tap', function() {
			$('#qrcode').empty();
			refreshCode(link + "?recommandPerson=" + storage.plainpassword + "&d=" + Math.random());
			return false;
		});

		function saveFile(strData) {
			setTimeout(function() {
				var filename = 'tuterm_' + (new Date()).getTime() + '.jpg';
				strData = strData.replace('image/jpeg', 'image/octet-stream');
				downLoadFile(strData, filename);
			}, 1000);
		}

		function downLoadFile(data, filename) {
			var _saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'),
				_event = document.createEvent('MouseEvents');
			_saveLink.href = data;
			_saveLink.download = filename;
			_event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
			_saveLink.dispatchEvent(_event);
		};

		function drawing() {
			var _canvas = document.getElementById("qrcode").getElementsByTagName("canvas")[0];
			var _imgSrc = _canvas.toDataURL("image/jpeg");
			saveFile(_imgSrc);
		}

		function drawing2() {
			var _canvas = document.getElementById("qrcode").getElementsByTagName("canvas")[0];
			var dataUrl = _canvas.toDataURL("image/jpeg");
			var b = new plus.nativeObj.Bitmap();
			b.loadBase64Data(dataUrl, function() {
				console.log("创建成功");
			}, function() {
				console.log("创建失败");
			});
			b.save('_www/img1.jpg', {
				overwrite: true
			}, function() {
				console.log("保存成功");
			}, function() {
				console.log("保存失败");
			});

			plus.gallery.save('_www/img1.jpg', function() {
				Util.openMessage('保存图片到相册成功');
			}, function() {
				Util.openMessage('保存图片到相册失败');
			});
		}
		$('#copy-img').on('tap', function() {
			//var myCanvas = document.getElementById("qrcode").getElementsByTagName("canvas")[0];  
			//var image = myCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");   
			//window.location.href=image; // it will save locally 
			if(isH5 || !window.plus) {
				drawing();
			} else {
				drawing2();
			}
		});
	},
	loadData: function() {
		$('#user-name').text(storage.truename);
		refreshCode(link + "?recommandPerson=" + storage.plainpassword);
	}
}
/**
 * 刷新二维码
 * @param {Object} url
 */
function refreshCode(url) {
	/*var qrcode = new QRCode(document.getElementById("qrcode"), {
		width: 100,
		height: 100
	});
	qrcode.makeCode(url);*/

	//	 jQuery('#qrcode').qrcode({
	//		     	 render    : "canvas",
	//		         text    : url,
	//		         width : "200",               //二维码的宽度
	//               height : "200",              //二维码的高度
	//               background : "#ffffff",       //二维码的后景色
	//               foreground : "#000000",        //二维码的前景色
	//               src: '../assets/img/binhe-logo.png'             //二维码中间的图片
	//		     });    

	var options = {
		render: 'canvas',
		//            ecLevel: jq('#eclevel').val(),
		ecLevel: 'H',
		//            minVersion: parseInt(jq('#minversion').val(), 10),
		minVersion: 8,
		//            fill: jq('#fill').val(),
		fill: '#333333',
		//            background: jq('#background').val(),
		background: '#eef8fe',
		// fill: jq('#img-buffer')[0],

		//            text: jq('#text').val(),//显示的文本
		text: url, //显示的文本
		//            size: parseInt(jq('#size').val(), 10),
		size: 300, //图片大小
		//            radius: parseInt(jq('#radius').val(), 10) * 0.01,
		radius: 50 * 0.01,
		//            quiet: parseInt(jq('#quiet').val(), 10),
		quiet: 4,
		//            mode: parseInt(jq('#mode').val(), 10),
		mode: 4, //图片 box

		//            mSize: parseInt(jq('#msize').val(), 10) * 0.01,
		mSize: 20 * 0.01, //字体图片大小
		//            mPosX: parseInt(jq('#mposx').val(), 10) * 0.01,
		mPosX: 50 * 0.01,
		//            mPosY: parseInt(jq('#mposy').val(), 10) * 0.01,
		mPosY: 50 * 0.01,
		//            label: jq('#label').val(),
		//            fontname: jq('#font').val(),
		label: storage.truename,
		fontname: 'Ubuntu',
		//            fontcolor: jq('#fontcolor').val(),
		fontcolor: '#1515EA',
		image: jQuery('#img-buffer')[0]
		// image : jQuery('<img id="img-buffer" src="../assets/img/binhe-logo.png">')[0]
	};
	jQuery('#qrcode').empty().qrcode(options);
}